const { src, dest, series, parallel, watch } = require("gulp")
// 删除文件
const del = require("del")
// 引入 ES6 转换插件
const babel = require("gulp-babel")
// 压缩 JS 文件
const uglify = require("gulp-uglify")
// 压缩 HTML
const htmlmin = require("gulp-htmlmin")
// 将 less 转换成 CSS
const less = require("gulp-less")
// 将 scss 转换成 CSS
var sass = require("gulp-sass")(require("sass"))
// 压缩 css
const csso = require("gulp-csso")
// 运行网络服务器的 Gulp 插件
const connect = require("gulp-connect")
// 处理 HTML 模板
const fileinclude = require("gulp-file-include")
const configPath = {
    jsPath: "./src/js/",
    jsLibs: "./src/libs/",
    htmlPath: "./src/",
    adminHtmlPath: "./src/admin/",
    uploadPath: "./src/upload/",
    cssPath: "./src/css/",
    imgPath: "./src/img/",
    mp3Path: "./src/mp3/",
    destHtml: "./dist",
    destAdminHtml: "./dist/admin",
    destCss: "./dist/css",
    destImg: "./dist/img",
    destMp3: "./dist/mp3",
    destJs: "./dist/js",
    destJsLibs: "./dist/libs",
    destUpload: "./dist/upload",
}
function upload() {
    return src(configPath.uploadPath + "**")
        .pipe(dest(configPath.destUpload))
        .pipe(connect.reload())
}
function adminHtml() {
    return src(configPath.adminHtmlPath + "*.html")
        .pipe(
            fileinclude({
                prefix: "@@",
                basepath: "@file",
            })
        )
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(dest(configPath.destAdminHtml))
        .pipe(connect.reload())
}
/*----------------------------- JS ----------------------------*/
function js() {
    return (
        src(configPath.jsPath + "**")
            // ES6 转换
            .pipe(babel({ presets: ["@babel/env"] }))
            // 压缩 JS
            .pipe(uglify())
            .pipe(dest(configPath.destJs))
            .pipe(connect.reload())
    )
}
exports.js = js
/*------------------------------libs----------------------------*/
function copyLibs() {
    return src(configPath.jsLibs + "**")
        .pipe(dest(configPath.destJsLibs))
        .pipe(connect.reload())
}
exports.copyLibs = copyLibs
/*----------------------------- HTML ----------------------------*/
function html() {
    return src("./src/*.html")
        .pipe(
            fileinclude({
                prefix: "@@",
                basepath: "@file",
            })
        )
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(dest("./dist"))
        .pipe(connect.reload())
}
exports.html = html
/*----------------------------- CSS ----------------------------*/
function css() {
    return (
        src([
            configPath.cssPath + "*.less",
            configPath.cssPath + "*.scss",
            configPath.cssPath + "*.css",
        ])
            // 转换 less
            .pipe(less())
            // 转换 scss
            .pipe(sass().on("error", sass.logError))
            .pipe(csso())
            .pipe(dest(configPath.destCss))
            .pipe(connect.reload())
    )
}
exports.css = css
/*----------------------imgs--------------------------*/
function copyImg() {
    return src(configPath.imgPath + "*")
        .pipe(dest(configPath.destImg))
        .pipe(connect.reload())
}
exports.copyImg = copyImg

// clean
function clean() {
    return del([configPath.destHtml + "/**"]).then(() => {
        console.log("项目初始化清理完成...")
    })
}
exports.clean = clean
/*
    开发模式 启动服务
*/
// watch
function watchList() {
    watch(configPath.htmlPath + "*.html", series(html))
    watch(configPath.htmlPath + "**", series(adminHtml))
    watch(configPath.cssPath + "*.scss", series(css))
    watch(configPath.cssPath + "*.less", series(css))
    watch(configPath.cssPath + "*.css", series(css))
    watch(configPath.imgPath + "*", series(copyImg))
    watch(configPath.jsPath + "**", series(js))
    watch(configPath.jsLibs + "*", series(copyLibs))
    watch(configPath.uploadPath + "**", series(upload))
}
// local server
let serverOptions = {
    name: "development",
    root: "./dist",
    host: "127.0.0.1",
    port: 8000,
    livereload: true,
}
function server() {
    connect.server(serverOptions)
}
exports.server = series(...init(), parallel(server, watchList))
// init
function init() {
    return [
        clean,
        parallel(html, adminHtml, css, upload, copyImg, js, copyLibs),
    ]
}
/*
    默认 组合 打包 所有任务
*/
exports.default = series(
    clean,
    parallel(html, adminHtml, css, js, copyImg, copyLibs, upload)
)
